﻿using System;
using System.Collections.Generic;
using CiStatus.CiStatusAddIn.Logic.Exceptions;
using CiStatus.CiStatusAddIn.Logic.Parameters;
using CiStatus.CiStatusAddIn.Logic.PluginInterfaces;

namespace CCNet157
{
    public class CcNetPollerFactory : ICiPollerFactory
    {
        public ICiPoller CreatePoller(IDictionary<string, string> settings)
        {
        	var ret = CreateEmptyParameters();
			ret["Server"].SetValue(GetValue(settings, "Server"));
			ret["Project"].SetValue(GetValue(settings, "Project"));
			ret["Link"].SetValue(GetValue(settings, "Link"));
			var poller = new CcNetPoller(ret);
            return poller;
        }

        private string GetValue(IDictionary<string, string> settings, string setting)
        {
            string server;
            settings.TryGetValue(setting, out server);
           
            if(!string.IsNullOrEmpty(server))
            {
                return settings[setting];
            }
            throw new CreatePollerException(string.Format("Error in setting key: {0}", setting));
        }

        public string Name
        {
            get { return "CCnet 1.5.7"; }
        }

        public IDictionary<string, ICiPollerParameter> EmptyParameters
        {
            get { return CreateEmptyParameters(); }
        }

        public IDictionary<string, ICiPollerParameter> CreateEmptyParameters()
    	{
    		var ret = new Dictionary<string, ICiPollerParameter>();
    		ret["Server"] = new SimpleKeyValueStringParameter(string.Empty);
    		ret["Project"] = new SimpleKeyValueStringParameter(string.Empty);
    		ret["Link"] = new SimpleKeyValueStringParameter(string.Empty);
    		return ret;
    	}
    }
}
